var fs = require('fs');
var moment = require('moment');
moment.locale("zh-cn");



var config = fs.readFileSync('./config.json').toString();
config = JSON.parse(config);
for (var i = 0; i < config.length; i++) {
    changeFile(config[i]);
}

function changeFile(path) {
    if (fs.existsSync(path)) {
        if (fs.statSync(path).isDirectory()) {
            var files = fs.readdirSync(path);
            files.forEach(element => {
                var current = path + "/" + element;
                if (fs.statSync(current).isDirectory()) {
                    changeFile(current);
                } else {
                    var prex = current.substr(-4);
                    if (prex == ".htm" || prex == "html") {
                        // console.log(moment().format("YYYYMMDDHHmmssSSS")+"-----"+current);
                        changeVersion(current);
                    }
                }

            });
        } else {
            changeVersion(path);
        }
    }
}


function changeVersion(path) {
    fs.readFile(path, 'utf8', function (err, data0) {
        if (err) throw err;
        var changenum = 0;
        var cssversion = [];
        var csspath = [];
        var jsversion = [];
        var jspath = [];
        // console.log(path);
        var path_prefix = path.substring(0, path.lastIndexOf("/"));
        //获取css相关
        var csstag = "href=\"[^\"]*\\.css(\\?v=[^\"]*)?\"";
        var regcsstag = new RegExp(csstag, "g");
        var css_result;
        while ((css_result = regcsstag.exec(data0)) != null) {
            // console.log(css_result);
            var cssallpath = css_result[0].split("?");
            if (css_result[1]) {
                cssversion.push(css_result[1].substring(3, css_result[1].length));
                csspath.push(cssallpath[0].substring(6, cssallpath[0].length));
            } else {
                cssversion.push("0");
                csspath.push(cssallpath[0].substring(6, cssallpath[0].length - 1));
            }


        }
        // console.log(cssversion);
        // console.log(csspath);

        //获取js相关
        var jstag = "src=\"[^\"]*\\.js(\\?v=[^\"]*)?\"";
        var regjstag = new RegExp(jstag, "g");
        var js_result;
        while ((js_result = regjstag.exec(data0)) != null) {
            // console.log(js_result);
            var jsallpath = js_result[0].split("?");
            if (js_result[1]) {
                jsversion.push(js_result[1].substring(3, js_result[1].length));
                jspath.push(jsallpath[0].substring(5, js_result[0].length));
            } else {
                jsversion.push("0");
                jspath.push(jsallpath[0].substring(5, js_result[0].length - 1));
            }

        }
        // console.log(jsversion);
        // console.log(jspath);



        for (var i = 0; i < csspath.length; i++) {
            var str = fs.statSync(path_prefix + "/" + csspath[i]).mtime;
            str = moment(str).format("YYYYMMDDHHmmss");
            if (str != cssversion[i]) {
                var c = "\"" + csspath[i] + "(\\?v=[^\"]*)?\"";
                var reg = new RegExp(c, "g");
                data0 = data0.replace(reg, "\"" + csspath[i] + "?v=" + str + "\"");
                // console.log("发生变化文件：" + csspath[i]);
                changenum += 1;
            }
        }

        for (var i = 0; i < jspath.length; i++) {
            if (jspath[i].substr(0, 4) == "http") {
                continue;
            }
            var str = fs.statSync(path_prefix + "/" + jspath[i]).mtime;
            str = moment(str).format("YYYYMMDDHHmmss");
            if (str != jsversion[i]) {
                var c = "\"" + jspath[i] + "(\\?v=[^\"]*)?\"";
                var reg = new RegExp(c, "g");
                data0 = data0.replace(reg, "\"" + jspath[i] + "?v=" + str + "\"");
                // console.log("发生变化文件：" + jspath[i]);
                changenum += 1;
            }
        }

        //写入到index.js
        if (changenum != 0) {
            // console.log("----"+fs+"----"+path+"----"+data0);
           //异步写入有时候会失败
            /*fs.writeFile(path, data0, function (err) {
                console.log("write error:"+err+"----"+path);
                if (err) throw err;
                console.log("处理成功！");
                console.log("变化文件个数：" + changenum);

            });*/
            fs.writeFileSync(path,data0);
            // console.log("处理成功！");
            console.log(path+"--变化文件个数：" + changenum);
        } else {
            console.log(path+"---没有变化文件！");
        }

    });
}